/**
 * 
 */
package com.psytech.app.task.driver.task.impl.stm;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.namespace.QName;
import javax.xml.ws.Holder;

import taskmanager.psytech.com.taskmanagerservice._1.Action;
import taskmanager.psytech.com.taskmanagerservice._1.BusinessFaultMsg;
import taskmanager.psytech.com.taskmanagerservice._1.ObjectFactory;
import taskmanager.psytech.com.taskmanagerservice._1.PriorityType;
import taskmanager.psytech.com.taskmanagerservice._1.StatusType;
import taskmanager.psytech.com.taskmanagerservice._1.TaskManagerService_Service;
import taskmanager.psytech.com.taskmanagerservice._1.TechnicalFaultMsg;

import com.psytech.app.task.web_ui.Comment;
import com.psytech.app.task.web_ui.Task;
import com.psytech.app.task.web_ui.Task.PRIORITY;
import com.psytech.app.task.web_ui.Task.STATUS;
import com.psytech.app.task.web_ui.User;
import com.psytech.app.task.web_ui.driver.TaskDriverInterface;
import com.psytech.app.task.web_ui.driver.exception.InvalidCriteria;
import com.psytech.app.task.web_ui.driver.exception.ObjectNotFoundException;

/**
 * @author ben
 */
public class StmTaskDriver implements TaskDriverInterface {

	private String wsdlUri;

	private static Logger LOG = Logger.getLogger(StmTaskDriver.class.getPackage().getName());

	private final static ObjectFactory OBJECT_FACTORY = new ObjectFactory();

	private taskmanager.psytech.com.taskmanagerservice._1.TaskManagerService managerService;

	private taskmanager.psytech.com.taskmanagerservice._1.TaskManagerService getTaskManagerServiceInstance() {
		if (this.managerService == null) {
			synchronized (StmTaskDriver.class) {
				if (this.managerService == null) {
					if (this.wsdlUri == null)
						throw new IllegalStateException("serviceUri must be set before invoking any getter");
					try {
						LOG.info("wsdlLocation " + this.wsdlUri);
						final TaskManagerService_Service managerServiceService = new TaskManagerService_Service(
								new URL(this.wsdlUri), new QName("http://com.psytech.taskmanager/TaskManagerService/1",
										"TaskManagerService"));
						this.managerService = managerServiceService.getTaskManagerServiceSOAP();
					} catch (final MalformedURLException e) {
						throw new IllegalStateException("Probléme lors de l'initialisation du service", e);
					}
				}
			}
		}
		return this.managerService;
	}

	@Override
	public void setServiceUri(final String serviceUri) {
		this.wsdlUri = serviceUri;
	}

	@Override
	public void setUserName(final String userName) {
		// Non utilisé

	}

	@Override
	public void setPassword(final String password) {
		// Non utilisé

	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#searchTask(com.psytech.app.task.web_ui.Task)
	 */
	@Override
	public List<Task> searchTask(final Task taskCriteria) throws InvalidCriteria {
		final List<Task> tasks = new ArrayList<Task>();
		try {
			for (final taskmanager.psytech.com.taskmanagerservice._1.Task task : this.getTaskManagerServiceInstance()
					.getTasks(null, this.taskToWsTask(taskCriteria)).getTask()) {
				tasks.add(this.wsTaskToTask(task));
			}
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new InvalidCriteria(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
		return tasks;
	}

	@Override
	public List<Task> searchTasksAssignedToUsers(final User user) throws ObjectNotFoundException,
			IllegalArgumentException {
		if (user == null)
			throw new IllegalArgumentException("The user can not be null");
		try {
			final taskmanager.psytech.com.taskmanagerservice._1.User wsUser = OBJECT_FACTORY.createUser();
			wsUser.setUid(user.getUid());
			final List<taskmanager.psytech.com.taskmanagerservice._1.Task> wsTasks = this
					.getTaskManagerServiceInstance().getTasks(wsUser, null).getTask();
			final List<Task> tasks = new ArrayList<Task>();
			for (final taskmanager.psytech.com.taskmanagerservice._1.Task task : wsTasks) {
				tasks.add(this.wsTaskToTask(task));
			}
			return tasks;
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public List<Task> searchNotAssignedTasks(final Task taskCriteria) throws ObjectNotFoundException,
			IllegalArgumentException {
		if (taskCriteria == null)
			throw new IllegalArgumentException("The task criteria can not be null");
		try {
			final taskmanager.psytech.com.taskmanagerservice._1.User wsUser = OBJECT_FACTORY.createUser();
			final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask = this.taskToWsTask(taskCriteria);
			List<taskmanager.psytech.com.taskmanagerservice._1.Task> wsTasks;
			wsTasks = this.getTaskManagerServiceInstance().getTasks(wsUser, wsTask).getTask();
			final List<Task> tasks = new ArrayList<Task>();
			for (final taskmanager.psytech.com.taskmanagerservice._1.Task task : wsTasks) {
				tasks.add(this.wsTaskToTask(task));
			}
			return tasks;
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage(), exception);
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		}
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#removeTask(java.lang.String)
	 */
	@Override
	public void deleteTask(final Task task) throws ObjectNotFoundException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		else if (task.getUid() == null)
			throw new IllegalArgumentException("taskUid can not be null");

		try {
			final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask = OBJECT_FACTORY.createTask();
			wsTask.setUid(task.getUid());
			final Holder<taskmanager.psytech.com.taskmanagerservice._1.Task> holder = new Holder<taskmanager.psytech.com.taskmanagerservice._1.Task>();
			holder.value = wsTask;
			this.getTaskManagerServiceInstance().manageTask(holder, Action.DELETE);
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage(), exception);
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		}

	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#getCommentsByTask(java.lang.String)
	 */
	@Override
	public List<Comment> getCommentsByTask(final Task task) throws ObjectNotFoundException, IllegalArgumentException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		else if (task.getUid() == null)
			throw new IllegalArgumentException("taskUid can not be null");
		final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask = OBJECT_FACTORY.createTask();
		wsTask.setUid(task.getUid());
		final List<Comment> comments = new ArrayList<Comment>();
		try {
			for (final taskmanager.psytech.com.taskmanagerservice._1.Comment comment : this
					.getTaskManagerServiceInstance().getComments(wsTask).getComment()) {
				comments.add(this.wsCommentToComment(comment));
			}

		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage(), exception);
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		} catch (final Exception e) {
			LOG.log(Level.WARNING, "Error on comment fetching", e);
			throw new RuntimeException();
		}

		return comments;
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#insertComment(com.psytech.app.task.web_ui.Comment)
	 */
	@Override
	public Comment createComment(final Comment comment) throws IllegalArgumentException, ObjectNotFoundException {
		final taskmanager.psytech.com.taskmanagerservice._1.Comment commentToCreate = OBJECT_FACTORY.createComment();
		final taskmanager.psytech.com.taskmanagerservice._1.User writer = OBJECT_FACTORY.createUser();
		writer.setUid(comment.getWriterUid());
		commentToCreate.setWriter(writer);
		commentToCreate.setText(comment.getText());
		final taskmanager.psytech.com.taskmanagerservice._1.Task task = OBJECT_FACTORY.createTask();
		task.setUid(comment.getTaskUid());
		commentToCreate.setTask(task);
		try {
			commentToCreate.setCreationDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(
					new GregorianCalendar()));
		} catch (final DatatypeConfigurationException e) {
			throw new RuntimeException(e);
		}
		final Holder<taskmanager.psytech.com.taskmanagerservice._1.Comment> holder = new Holder<taskmanager.psytech.com.taskmanagerservice._1.Comment>();
		holder.value = commentToCreate;
		try {
			this.getTaskManagerServiceInstance().manageComment(holder, Action.CREATE);
			comment.setUid(holder.value.getUid());
			return comment;
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		} catch (final Exception e) {
			LOG.log(Level.SEVERE, "Error on comment management", e);
			throw new RuntimeException();
		}
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#removeComment(java.lang.String)
	 */
	@Override
	public void deleteComment(final Comment comment) throws ObjectNotFoundException, IllegalArgumentException {
		if (comment == null)
			throw new IllegalArgumentException("comment can not be null");
		else if (comment.getUid() == null)
			throw new IllegalArgumentException("comment uid con not be null");
		try {
			final taskmanager.psytech.com.taskmanagerservice._1.Comment wsComment = OBJECT_FACTORY.createComment();
			wsComment.setUid(comment.getUid());
			final Holder<taskmanager.psytech.com.taskmanagerservice._1.Comment> holder = new Holder<taskmanager.psytech.com.taskmanagerservice._1.Comment>();
			holder.value = wsComment;
			this.getTaskManagerServiceInstance().manageComment(holder, Action.DELETE);
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	private Task wsTaskToTask(final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask) {
		assert wsTask != null;
		final Task tmpTask = new Task();
		tmpTask.setUid(wsTask.getUid());
		tmpTask.setName(wsTask.getName());
		if (wsTask.getDescription() != null && !wsTask.getDescription().isNil()) {
			tmpTask.setDescription(wsTask.getDescription().getValue());
		}
		tmpTask.setPriority(PRIORITY.valueOf(wsTask.getPriority().toString()));
		tmpTask.setStatus(STATUS.valueOf(wsTask.getStatus().toString()));
		if (wsTask.getExpectedEndDate() != null) {
			tmpTask.setExpectedEndDate(wsTask.getExpectedEndDate().getValue().toGregorianCalendar().getTime());
		}
		if (wsTask.getExpectedStartDate() != null) {
			tmpTask.setExpectedStartDate(wsTask.getExpectedStartDate().getValue().toGregorianCalendar().getTime());
		}
		tmpTask.setCreatorUid(wsTask.getCreator().getUid());
		if (wsTask.getAffectedUsers() != null && wsTask.getAffectedUsers().size() > 0) {
			tmpTask.setAssignedUserUid(wsTask.getAffectedUsers().get(0).getUid());
		}
		assert wsTask.getUid() != null;
		return tmpTask;
	}

	private taskmanager.psytech.com.taskmanagerservice._1.Task taskToWsTask(final Task task) {
		assert task != null;
		final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask = OBJECT_FACTORY.createTask();
		wsTask.setUid(task.getUid());
		wsTask.setName(task.getName());
		if (task.getDescription() != null) {
			wsTask.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		}
		if (task.getPriority() != null) {
			wsTask.setPriority(PriorityType.fromValue(task.getPriority().toString()));
		}
		if (task.getStatus() != null) {
			wsTask.setStatus(StatusType.fromValue(task.getStatus().toString()));
		}
		if (task.getExpectedEndDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				wsTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Problème de conversion de date", exception);
			}
		}
		if (task.getExpectedStartDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedStartDate());
				wsTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Problème de conversion de date", exception);
			}
		}
		if (task.getCreatorUid() != null) {
			final taskmanager.psytech.com.taskmanagerservice._1.User creator = OBJECT_FACTORY.createUser();
			creator.setUid(task.getCreatorUid());
			wsTask.setCreator(creator);
		}
		if (task.getAssignedUserUid() != null) {
			final taskmanager.psytech.com.taskmanagerservice._1.User assignedUser = OBJECT_FACTORY.createUser();
			assignedUser.setUid(task.getAssignedUserUid());
			wsTask.getAffectedUsers().add(assignedUser);
		}
		return wsTask;
	}

	private Comment wsCommentToComment(final taskmanager.psytech.com.taskmanagerservice._1.Comment wsComment) {
		assert wsComment != null;
		final Comment comment = new Comment();
		comment.setUid(wsComment.getUid());
		comment.setText(wsComment.getText());
		comment.setWriterUid(wsComment.getWriter().getUid());
		comment.setCreationDate(wsComment.getCreationDate().toGregorianCalendar().getTime());
		comment.setTaskUid(wsComment.getTask().getUid());
		return comment;

	}

	@Override
	public Task assignsTaskToUser(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.getAffectedUsers().clear();
		if (task.getAssignedUserUid() != null) {
			final taskmanager.psytech.com.taskmanagerservice._1.User affectedUser = OBJECT_FACTORY.createUser();
			affectedUser.setUid(task.getAssignedUserUid());
			existingTask.getAffectedUsers().add(affectedUser);
		}
		return this.updateTask(existingTask);

	}

	@Override
	public Task beginTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setStatus(StatusType.PROGRESS);
		return this.updateTask(existingTask);

	}

	@Override
	public Task createTask(final Task task) throws IllegalArgumentException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		final taskmanager.psytech.com.taskmanagerservice._1.Task taskToCreate = OBJECT_FACTORY.createTask();

		taskToCreate.setUid(task.getUid());
		taskToCreate.setName(task.getName());
		taskToCreate.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		taskToCreate.setPriority(PriorityType.fromValue(task.getPriority().toString()));
		taskToCreate.setStatus(StatusType.PENDING);
		if (task.getExpectedEndDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				taskToCreate.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
		}
		if (task.getExpectedStartDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedStartDate());
				taskToCreate.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory
						.newInstance().newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
		}
		final taskmanager.psytech.com.taskmanagerservice._1.User creator = OBJECT_FACTORY.createUser();
		creator.setUid(task.getCreatorUid());
		taskToCreate.setCreator(creator);

		final Holder<taskmanager.psytech.com.taskmanagerservice._1.Task> tasks = new Holder<taskmanager.psytech.com.taskmanagerservice._1.Task>();
		tasks.value = taskToCreate;
		try {
			this.managerService.manageTask(tasks, Action.CREATE);
			return this.wsTaskToTask(tasks.value);
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task completeTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setStatus(StatusType.COMPLETE);
		return this.updateTask(existingTask);
	}

	@Override
	public Task suspendTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setStatus(StatusType.PENDING);
		return this.updateTask(existingTask);
	}

	@Override
	public Task updateExpectedEndDate(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		if (task.getExpectedEndDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				existingTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
		} else {
			existingTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(null));
		}

		return this.updateTask(existingTask);
	}

	@Override
	public Task updateExpectedStartDate(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		if (task.getExpectedStartDate() != null) {
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedStartDate());
				existingTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory
						.newInstance().newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
		} else {
			existingTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(null));
		}

		return this.updateTask(existingTask);
	}

	@Override
	public Task updateTaskDescription(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		return this.updateTask(existingTask);
	}

	@Override
	public Task updateTaskName(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		LOG.log(Level.FINE, task.getName());
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setName(task.getName());
		return this.updateTask(existingTask);
	}

	@Override
	public Task increasePriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setPriority(PriorityType.HIGH);
		return this.updateTask(existingTask);
	}

	@Override
	public Task lowPriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setPriority(PriorityType.LOW);
		return this.updateTask(existingTask);
	}

	@Override
	public Task unSpecifiPriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		this.checkTaskUid(task);
		final taskmanager.psytech.com.taskmanagerservice._1.Task existingTask = this.getTaskByUid(task.getUid());
		existingTask.setPriority(PriorityType.MEDIUM);
		return this.updateTask(existingTask);
	}

	private void checkTaskUid(final Task task) {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		if (task.getUid() == null)
			throw new IllegalArgumentException("task uid can not be null");
	}

	private taskmanager.psytech.com.taskmanagerservice._1.Task getTaskByUid(final String uid)
			throws ObjectNotFoundException {
		assert uid != null;
		LOG.log(Level.FINE, uid);
		try {
			final taskmanager.psytech.com.taskmanagerservice._1.Task wsTask = OBJECT_FACTORY.createTask();
			wsTask.setUid(uid);
			final List<taskmanager.psytech.com.taskmanagerservice._1.Task> wsTasks = this
					.getTaskManagerServiceInstance().getTasks(null, wsTask).getTask();
			if (wsTasks == null || wsTasks.size() < 1) {
				LOG.log(Level.FINE, "No task found");
				throw new ObjectNotFoundException();
			}
			return wsTasks.get(0);
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	/*
	 * mes à jour une tache (taskmanager.psytech.com.taskmanagerservice._1.Task) dans le référenciel et renvoie son équivalant en Task
	 */
	private Task updateTask(final taskmanager.psytech.com.taskmanagerservice._1.Task task) {
		assert task != null;
		assert task.getUid() != null;
		final Holder<taskmanager.psytech.com.taskmanagerservice._1.Task> holder = new Holder<taskmanager.psytech.com.taskmanagerservice._1.Task>();
		holder.value = task;
		try {
			this.managerService.manageTask(holder, Action.UPDATE);
			return this.wsTaskToTask(holder.value);
		} catch (final BusinessFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFaultMsg exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

}
